<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.rtcoperator_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text" size="small" icon="el-icon-connection" @click.stop="handleBind(scope.row)">绑定</el-button>
        <el-button type="text" size="small" icon="el-icon-full-screen" @click.stop="handleCode(scope.row)">扫码</el-button>
      </template>
    </avue-crud>
    <el-dialog title="请选择通讯录人员" append-to-body :visible.sync="userBox">
      <el-table border stripe :data="dataUser">
        <el-table-column prop="username" label="登录账号" align="center"></el-table-column>
        <el-table-column prop="nickName" label="用户昵称" align="center"></el-table-column>
        <el-table-column prop="realName" label="用户姓名" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-connection" @click="handleBindB(scope.row)">绑定</el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
            <el-button @click="userBox = false">取 消</el-button>
          </span>
    </el-dialog>
    <transition name="dialog-fade">
      <div class="qr-mask" v-show="qrcodeShow">
        <div class="qr-box">
          <i class="el-icon-close" @click="closeQrCode"></i>
          <p class="title">请扫码</p>
          <img class="qrcode-wx" :src="qrcodeWx | fileFilter" alt="">
        </div>
      </div>
    </transition>
  </basic-container>
</template>

<script>
import {getPage, getDetail, add, update, remove, bind, token, qrCode} from "@/api/bureau/userofficer.js";
import {mapGetters} from "vuex";
import {userPage} from "@/api/system/user";
import CommonFilters from "@/mixins/CommonFilters";

export default {
  name: "index",
  data() {
    return {
      userBox: false,
      qrcodeShow: false,
      qrcodeWx: "",
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        selection: true,
        column: [
          {
            label: "警号",
            prop: "account",
            rules: [{
              required: true,
              message: "请输入警号",
              trigger: "blur"
            }]
          },
          {
            label: "姓名",
            prop: "name",
            rules: [{
              required: true,
              message: "请输入昵称",
              trigger: "blur"
            }]
          },
          {
            label: "手机",
            prop: "phone",
            rules: [{
              required: false,
              message: "请输入手机",
              trigger: "blur"
            }]
          },
          {
            label: "接警员编号",
            prop: "userId",
            disabled: true
          },
          {
            label: "排序",
            prop: "sort",
            type: "number"
          },
        ]
      },
      data: [],
      dataUser: [],
      accidentUserId: null,
    };
  },
  mixins: [CommonFilters],
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.rtcoperator_add, false),
        viewBtn: this.vaildData(this.permission.rtcoperator_view, false),
        delBtn: this.vaildData(this.permission.rtcoperator_delete, false),
        editBtn: this.vaildData(this.permission.rtcoperator_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    handleBind(row){
      userPage(1, 50).then(resuser => {
        this.dataUser = resuser.data.data;
        this.userBox = true;
        this.accidentUserId = row.id;
      });
    },
    handleBindB(row) {
      token(this.accidentUserId).then(restoken => {
        bind({
          id: this.accidentUserId,
          token: restoken.data.data.token,
          operatorId: row.id
        }).then(resbind => {
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        })
      });
    },
    handleCode(row) {
      this.qrcodeShow = true;
      let that = this;
      qrCode(row.id).then(function(response){
        console.log("-----", response);
        that.qrcodeWx = response.data.data.path;
      });
    },
    closeQrCode() {
      this.qrcodeShow = false;
      // this.queryData();
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.page.total = res.data.count;
        this.data = res.data.data;
        this.selectionClear();
      });
      this.loading = false;
    }
  }
}
</script>

<style scoped lang="scss">
.qr-mask {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 4;
  background-color: rgba(0, 0, 0, 0.6);
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;

  .qr-box{
    position: relative;
    .el-icon-close {
      font-size: 30px;
      cursor: pointer;
      color: #fff;
      position: absolute;
      right: -30px;
      top: -20px;
    }
  }

  .title {
    font-size: 26px;
    color: #fff;
    text-align: center;
  }

  .qrcode-wx {
    width: 240px;
    height: 270px;
  }
}
</style>
